﻿using System.Threading.Tasks;
using DailyPoetryX.Models;
using DailyPoetryX.Services;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Xamarin.Essentials;

namespace DailyPoetryX.ViewModels {
    public class TodayPageViewModel : ViewModelBase {
        /******** 构造函数 ********/

        private ITodayImageService todayImageService;
        private ITodayPoetryService todayPoetryService;
        private IContentNavigationService contentNavigationService;
        private IRootNavigationService rootNavigationService;

        public TodayPageViewModel(ITodayImageService todayImageService,
            ITodayPoetryService todayPoetryService,
            IContentNavigationService contentNavigationService,
            IRootNavigationService rootNavigationService) {
            this.todayImageService = todayImageService;
            this.todayPoetryService = todayPoetryService;
            this.contentNavigationService = contentNavigationService;
            this.rootNavigationService = rootNavigationService;
        }

        /******** 绑定属性 ********/

        public TodayImage TodayImage {
            get => _todayImage;
            set => Set(nameof(TodayImage), ref _todayImage, value);
        }

        private TodayImage _todayImage;

        public TodayPoetry TodayPoetry {
            get => _todayPoetry;
            set => Set(nameof(TodayPoetry), ref _todayPoetry, value);
        }

        private TodayPoetry _todayPoetry;

        public bool TodayPoetryLoaded {
            get => _todayPoetryLoaded;
            set => Set(nameof(TodayPoetryLoaded), ref _todayPoetryLoaded,
                value);
        }

        private bool _todayPoetryLoaded;

        public bool TodayPoetryLoading {
            get => _todayPoetryLoading;
            set => Set(nameof(TodayPoetryLoading), ref _todayPoetryLoading,
                value);
        }

        private bool _todayPoetryLoading;

        /******** 绑定命令 ********/

        public RelayCommand PageAppearingCommand =>
            _pageAppearingCommand ?? (_pageAppearingCommand = new RelayCommand(
                () => {
                    var run = false;

                    if (!pageLoaded) {
                        lock (pageLoadedLock) {
                            if (!pageLoaded) {
                                pageLoaded = true;
                                run = true;
                            }
                        }
                    }

                    if (!run) return;

                    Task.Run(async () => {
                        TodayImage =
                            await todayImageService.GetTodayImageAsync();

                        var updateResult =
                            await todayImageService.CheckUpdateAsync();
                        if (updateResult.HasUpdate) {
                            TodayImage = updateResult.TodayImage;
                        }
                    });

                    Task.Run(async () => {
                        TodayPoetryLoading = true;
                        TodayPoetryLoaded = false;
                        TodayPoetry =
                            await todayPoetryService.GetTodayPoetryAsync();
                        TodayPoetryLoading = false;
                        TodayPoetryLoaded = true;
                    });
                }));

        private RelayCommand _pageAppearingCommand;

        public RelayCommand ShowDetailCommand => _showDetailCommand ??
            (_showDetailCommand = new RelayCommand(async () =>
                await contentNavigationService.NavigateToAsync(
                    ContentNavigationServiceConstants.TodayDetailPage)));

        private RelayCommand _showDetailCommand;

        public RelayCommand JinrishiciCommand => _jinrishiciCommand ??
            (_jinrishiciCommand = new RelayCommand(async () =>
                await Browser.OpenAsync("https://www.jinrishici.com",
                    BrowserLaunchMode.SystemPreferred)));

        private RelayCommand _jinrishiciCommand;

        public RelayCommand CopyrightCommand => _copyrightCommand ??
            (_copyrightCommand = new RelayCommand(async () =>
                await Browser.OpenAsync(TodayImage.CopyrightLink,
                    BrowserLaunchMode.SystemPreferred)));

        private RelayCommand _copyrightCommand;

        public RelayCommand QueryCommand =>
            _queryCommand ?? (_queryCommand = new RelayCommand(async () => {
                var poetryQuery = new PoetryQuery {
                    Name = TodayPoetry.Name, AuthorName = TodayPoetry.AuthorName
                };
                await rootNavigationService.NavigateToAsync(
                    RootNavigationServiceConstants.QueryPage, poetryQuery);
            }));

        private RelayCommand _queryCommand;

        /******** 私有变量 ********/

        private bool pageLoaded;
        private readonly object pageLoadedLock = new object();
    }
}